//
//  Status.swift
//  mySina
//
//  Created by mac on 15/12/21.
//  Copyright © 2015年 tz. All rights reserved.
//

import UIKit

class Status: NSObject {
    //微博创建时间
    var created_at:String?
    //微博ID
    var id: Int64 = 0
    //微博信息内容
    var text: String?
    //微博来源
    var source: String?
    //用户模型
    var user:User?
    //配图的数组
    var pic_urls:[[String : String]]? {
        didSet {
            guard let array = pic_urls else {
                return
            }
            //遍历数组
            imageURLs = [NSURL]()
            for item in array {
                //相信后端，一定会给我们返回一个url的字段
                let urlString = item["thumbnail_pic"]!
                //相信后端给定的urlString  一定能够转换为 url对象
//                urlString = urlString.stringByReplacingOccurrencesOfString("thumbnail", withString: "large")
                let url = NSURL(string: urlString)!
                //添加uel
                imageURLs?.append(url)
            }
        }
    }
    //转发微博的信息
    var retweeted_status:Status?
    //将配图数组转换为 纯种的url对象
    var imageURLs:[NSURL]?
    //构造方法，利用KVC进行赋值
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String : AnyObject] {
                user = User(dict: dict)
//                print(user)
            }
            //此处需要加return，否则上面的字典转模型就白做了
            return
        }
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject] {
                retweeted_status = Status(dict: dict)
                return
            }
        }
        //给其他属性设置值
        super.setValue(value, forKey: key)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    override var description:String {
        let keys = ["created_at","id","text","source","user"]
        return dictionaryWithValuesForKeys(keys).description
    }
}